#define _WIN32_WINNT _WIN32_WINNT_WINBLUE

#include "stdafx.h"

MWApp myApp;

#define VIRTWINDOWSWIDTH 2000
#define SCROLLWIDTH 10

BOOL MWApp::InitInstance()
{
	/*** 
	  application Initialization
	*/

	m_pMainWnd = new CMainWindow;
	
	//Show and update Window
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	
	return TRUE;
}


int MWApp::ExitInstance()
{
	// cleanup application 

	return CWinApp::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_HSCROLL()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
	Create(NULL, _T("MFC Ruler"), WS_OVERLAPPEDWINDOW | WM_HSCROLL);

}

void CMainWindow::OnHScroll(UINT nCode, UINT nPos, CScrollBar *ScrollBar)
{
	CFrameWnd::OnHScroll(nCode, nPos, ScrollBar);

	int nDelta=0;
	
	SCROLLINFO si;
	this->GetScrollInfo(SB_HORZ, &si);

	switch (nCode)
	{
	case SB_LINELEFT:
		nDelta = -SCROLLWIDTH;
		break;
	case SB_LINERIGHT:
		nDelta = SCROLLWIDTH;
		break;
	case SB_PAGELEFT:
		nDelta = -m_nPagePos;
		break;
	case SB_PAGERIGHT:
		nDelta = m_nPagePos;
		break;
	case SB_THUMBTRACK:
		nDelta = static_cast<int>(nPos)-m_nScrollPos;
		break;
	case SB_THUMBPOSITION:
		break;
	default:
		return;
	}

	int nScrollPos = m_nScrollPos + nDelta;
	int maxpos = VIRTWINDOWSWIDTH -m_nPagePos;


	if (nScrollPos < 0)
	{
		nDelta = -m_nScrollPos;
	}
	else if (nScrollPos > maxpos)
	{
		nDelta = maxpos - m_nScrollPos;
	}

	if (nDelta != 0)
	{
		m_nScrollPos += nDelta;
		SetScrollPos(SB_HORZ, m_nScrollPos, TRUE);
		InvalidateRect(NULL);
		ScrollWindow(-nDelta, 0);
	}

}
	

void CMainWindow::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);

	int iScrollMax = VIRTWINDOWSWIDTH;
	m_nScrollPos = 0;
	m_nPagePos = cx;
	/***
	  Resize Scrollbar
	*/
	SCROLLINFO si;
	this->GetScrollInfo(SB_HORZ, &si);

	si.nPage = 0;
	si.nMax = 0;
	if (cx < iScrollMax)
	{
		si.nMax = iScrollMax - 1;
		si.nPage = cx; 
		si.nPos = min(m_nScrollPos, (iScrollMax -cx - 1));
	}

	si.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
	si.nMin = 0;

	this->SetScrollInfo(SB_HORZ, &si, TRUE);
	/***
	  End Resize Scrollbar
	*/

}

void CMainWindow::OnPaint()
{
	//CFrameWnd::OnPaint();

	CPaintDC dc(this);

	
	dc.SetMapMode(MM_LOENGLISH);
	
	dc.SetWindowOrg(m_nScrollPos, 0);

	dc.SetBkMode(TRANSPARENT);

	CBrush brush(RGB(255, 255, 200));
	CBrush *pOldBrush = dc.SelectObject(&brush);
	dc.Rectangle(100, -100, 1300, -200);
	dc.SelectObject(pOldBrush);
	int i;

	for (i = 125; i < 1300; i += 25){
		dc.MoveTo(i, -192);
		dc.LineTo(i, -200);
	}

	for (i = 125; i < 1300; i += 50){
		dc.MoveTo(i, -184);
		dc.LineTo(i, -200);
	}

	for (i = 125; i < 1300; i += 100){
		dc.MoveTo(i, -175);
		dc.LineTo(i, -200);

		
		CString string;
		string.Format(_T("%d"), (i / 100) - 1);
		dc.SetTextAlign(TA_CENTER | TA_BOTTOM);
		dc.TextOut(i, -175, string);

		
		CString string1;
		CRect rect;
		GetClientRect(&rect);
		string1.Format(_T("%d+%d=%d (%d)"), m_nScrollPos, rect.Width(), VIRTWINDOWSWIDTH, m_nScrollPos + rect.Width());
		dc.SetTextAlign(TA_LEFT | TA_BOTTOM);
		dc.TextOut(2 + m_nScrollPos, -12, string1);


	}

}
